<?php

namespace App\Http\Models;

use App\Observers\BosCleanObserver;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * @method self filterArticleTypeId(int $article_type_id)
 * @method self filterTitle(string $title)
 *
 * @property string title - 标题
 * @property string content - 内容 html
 * @property string keywords - SEO 关键字
 * @property string description - SEO 描述
 */
class Article extends Model
{
    use SafeSave,Bosable;

    public $bos_columns = ['thumb','carousel_figure'];

    protected $guarded = [''];

    protected $thumb_bos_key_prefix = 'article/thumb/';
//    protected $banner_bos_key_prefix = 'article/banner/';
    protected $carousel_bos_key_prefix = 'article/carousel/';
//    protected $company_bos_key_prefix = 'article/company/';

    protected $table = 'articles';

    protected $casts = [
        'article_type_id' => 'int'
    ];

    /**
     * @param $thumb
     */
    public function setThumbAttribute($thumb)
    {
        if (is_file($thumb)) {
            $this->attributes['thumb'] = $this->uploadPostFile($thumb , $this->thumb_bos_key_prefix);
        }
    }

    /**
     * @return mixed|null
     */
    public function getThumbAttribute()
    {
        if (! $this->attributes['thumb']) return null;

        return $this->getStaticUrl($this->attributes['thumb']);
    }

    /**
     * @deprecated
     *
     * @param $banner
     */
    public function setBannerAttribute($banner)
    {
        if (is_file($banner)) {
            $this->attributes['banner'] = $this->uploadPostFile($banner , $this->banner_bos_key_prefix);
        }
    }

    /**
     * @deprecated
     * @return mixed|null
     */
    public function getBannerAttribute()
    {
        if (! $this->attributes['banner']) return null;

        return $this->getStaticUrl($this->attributes['banner']);
    }

    /**
     * @deprecated
     * @param $carousel_figure
     */
    public function setCarouselFigureAttribute($carousel_figure)
    {
        if (is_file($carousel_figure)) {
            $this->attributes['carousel_figure'] = $this->uploadPostFile($carousel_figure , $this->carousel_bos_key_prefix);
        }
    }

    /**
     * @deprecated
     * @return mixed|null
     */
    public function getCarouselFigureAttribute()
    {
        if (! $this->attributes['carousel_figure']) return null;

        return $this->getStaticUrl($this->attributes['carousel_figure']);
    }

    /**
     * @deprecated
     * @param $company_thumb
     */
    public function setCompanyThumbAttribute($company_thumb)
    {
        if (is_file($company_thumb)) {
            $this->attributes['company_thumb'] = $this->uploadPostFile($company_thumb , $this->company_bos_key_prefix);
        }
    }

    /**
     * @deprecated
     * @return mixed|null
     */
    public function getCompanyThumbAttribute()
    {
        if (! $this->attributes['company_thumb']) return null;

        return $this->getStaticUrl($this->attributes['company_thumb']);
    }

    /**
     * @used-by filterArticleTypeId
     *
     * @param Builder $query
     * @param $article_type_id
     * @return Builder
     */
    public function scopeFilterArticleTypeId(Builder $query, $article_type_id)
    {
        if (!$article_type_id) return $query;

        return $query->where('article_type_id', $article_type_id);
    }

    /**
     * @param Builder $query
     * @param $title
     * @return Builder
     */
    public function scopeFilterTitle(Builder $query, $title)
    {
        if (!$title) return $query;

        return $query->where('title', 'like', '%'.$title.'%')
            ->orWhere('english_title', 'like', '%'.$title.'%');
    }

    public function scopeArticleTagId($query,$tag_id)
    {
        $tag = ArticleTag::find($tag_id);

        $article_ids = $tag->articles->pluck('id');

        if ( count($article_ids)==0 )
        {
            $article_ids = DB::table('article_pivot_tag')->whereIn('article_tag_id',$tag->tags->pluck('id'))->pluck('article_id');
        }

        return $query->whereIn( 'id' , $article_ids );
    }

    public function articleTags()
    {
        return $this->belongsToMany(ArticleTag::class,'article_pivot_tag','article_id','article_tag_id');
    }

    /**
     * @param $article_type_id
     * @return bool
     */
    public static function checkIsCompanyNews($article_type_id)
    {
        $article_types = ArticleTag::where('super_tag_id', ArticleTag::COMPANY_NEWS)->pluck('id');

        if ($article_types->contains($article_type_id)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *  注册删除bosKey事件绑定
     *
     */
    protected static function boot()
    {
        parent::boot();

        /*
         * 监听bos清理事件
         */
        self::observe((BosCleanObserver::class));
    }
}
